// 检查浏览器是否支持 navigator.mediaDevices API
if (navigator.mediaDevices && navigator.mediaDevices.enumerateDevices) {
    // 先请求媒体权限
    navigator.mediaDevices.getUserMedia({ audio: true, video: true })
        .then(function(stream) {
            // 之后获取并列出所有媒体设备
            return navigator.mediaDevices.enumerateDevices();
        })
        .then(function(devices) {
            let deviceInfoText = '';

            devices.forEach(function(device) {
                deviceInfoText += `Kind: ${device.kind}\n`;
                deviceInfoText += `Label: ${device.label}\n`;
                deviceInfoText += `Device ID: ${device.deviceId}\n`;
                deviceInfoText += `Group ID: ${device.groupId}\n`;
                deviceInfoText += '\n';

                var option = document.createElement('option');
                option.text = device.label;
                option.value = device.deviceId;

                if(device.kind === 'videoinput') {
                    document.getElementById('videoInput').appendChild(option);
                } else if(device.kind === 'audioinput') {
                    document.getElementById('audioInput').appendChild(option);
                } else if(device.kind === 'audiooutput') {
                    document.getElementById('audioOutput').appendChild(option);
                }
            });

            // 将设备信息输出到页面上
            document.getElementById('deviceInfo').textContent = deviceInfoText;
        })
        .catch(function(err) {
            console.error('Error accessing devices:', err);
        });
} else {
    console.log("navigator.mediaDevices.enumerateDevices() is not supported.");
}
